#include "MCU_FLash.h"

uint8_t MCU_Flash_Erase(uint32_t Sector_Start, uint32_t Sector_End)
{
    FLASH_EraseInitTypeDef Erase_Init_Struct;
    uint32_t Sector_Error = 0;
    HAL_FLASH_Unlock();
    Erase_Init_Struct.TypeErase = FLASH_TYPEERASE_SECTORS;
    Erase_Init_Struct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
    Erase_Init_Struct.Sector = Sector_Start;
    Erase_Init_Struct.NbSectors = Sector_End;

    if (HAL_FLASHEx_Erase(&Erase_Init_Struct, &Sector_Error) != HAL_OK)
    {
        return 0;
    }
    HAL_FLASH_Lock();
    return 1;
}

uint8_t MCU_Flash_Writedata(uint8_t *data, uint32_t len, uint32_t address)
{
    HAL_FLASH_Unlock();
    uint32_t i = 0;

    for (i = 0; i < len; i++)
    {
        if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, (uint32_t)address, *data) == HAL_OK)
        {
            address++;
            data++;
        }
        else
        {
            return 0;
        }
    }
    HAL_FLASH_Lock();
    return 1;
}
